﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using System.Management;

using Consejo.Monitoring.BusinessObjects;
using Consejo.Monitoring.ApplicationHooks;
using Consejo.Monitoring.Destinations;

namespace WinMonitoringAgent
{
    public partial class MonitorTestInterface : Form
    {
        public MonitorTestInterface()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            List<IStatusItem> _statusItems;
            List<ServerStatus> _servers;
            SystemStatus _systemStatus;

            /* FIRST TEST */

            _servers = new List<ServerStatus>();


            _statusItems = new List<IStatusItem>();
            NameValueCollection SvcNvc = (NameValueCollection)System.Configuration.ConfigurationManager.GetSection("AgentSettings/ServiceValues");
            for (int i = 0; i < SvcNvc.Count; i++)
            {
                bool svcStarted = false;
                string svcID = SvcNvc.GetKey(i);
                string svcName = SvcNvc.Get(i);
                bool IsReachable = false;
                double ResponseValue = 0.00;
                UInt32 PingVal = 0;
                if (SvcNvc.Get(i) == ("PingVal"))
                {
                    string Gateway = System.Configuration.ConfigurationManager.AppSettings["Gateway"];
                    Consejo.Monitoring.ApplicationHooks.Values.ResponseTime(out PingVal, out IsReachable, Gateway);
                    ResponseValue = Convert.ToDouble(PingVal);

                    _statusItems.Add(new ServiceStatus(svcID, IsReachable, ResponseValue));
                }
                else
                {
                    Consejo.Monitoring.ApplicationHooks.Values.ServiceValues(svcName, out svcStarted);
                    _statusItems.Add(new ServiceStatus(svcID, svcStarted, 5));
                }

            }

            NameValueCollection ResourceNvc = (NameValueCollection)System.Configuration.ConfigurationManager.GetSection("AgentSettings/ResourceValues");
            for (int i = 0; i < ResourceNvc.Count; i++)
            {
                Double FreeSpace = 0.00;
                bool IsReachable = false;
                double cpuLoad = 0.00;
                UInt32 PingVal = 0;
                UInt64 MemoryVal = UInt64.MinValue;
                UInt64 FreeMemoryVal = UInt64.MinValue;
                string ResourceName = ResourceNvc.Get(i);
                string ResourceID = ResourceNvc.GetKey(i);
                double ResourceValue = double.MinValue;
                int SPSiteCount = 0;

                if (ResourceName == "cpu")
                {
                    Consejo.Monitoring.ApplicationHooks.Values.ProcessorValues(out cpuLoad);
                    ResourceValue = Convert.ToDouble(cpuLoad);
                }
                else
                    if (ResourceName == "PingVal")
                    {
                        string Gateway = System.Configuration.ConfigurationManager.AppSettings["Gateway"];
                        Consejo.Monitoring.ApplicationHooks.Values.ResponseTime(out PingVal, out IsReachable, Gateway);
                        ResourceValue = Convert.ToDouble(PingVal);
                    }
                    else
                        if (ResourceName == "Memory")
                        {
                            Consejo.Monitoring.ApplicationHooks.Values.Memory(out MemoryVal, out FreeMemoryVal);
                            ResourceValue = Convert.ToDouble(MemoryVal);
                        }
                        else
                            if (ResourceName == "FreeMemory")
                            {
                                Consejo.Monitoring.ApplicationHooks.Values.Memory(out MemoryVal, out FreeMemoryVal);
                                ResourceValue = Convert.ToDouble(FreeMemoryVal);
                            }
                            else
                                if (ResourceName == "SiteCollection")
                                {
                                    string spsite = System.Configuration.ConfigurationManager.AppSettings["SiteCollection"];
                                    Consejo.Monitoring.ApplicationHooks.SPValues.GetSPSiteCount(out SPSiteCount, spsite);
                                    ResourceValue = Convert.ToDouble(SPSiteCount);
                                }
                                else
                                    if (ResourceName == "MemoryW3WP")
                                    {
                                        Consejo.Monitoring.ApplicationHooks.Values.Memory(out MemoryVal, out FreeMemoryVal);
                                        ResourceValue = Convert.ToDouble(MemoryVal);
                                    }
                _statusItems.Add(new ResourceStatus(ResourceID, ResourceValue));

            }
            NameValueCollection DiskNvc = (NameValueCollection)System.Configuration.ConfigurationManager.GetSection("AgentSettings/DiskValues");
            for (int i = 0; i < DiskNvc.Count; i++)
            {
                Double FreeSpace = 0.00;
                string DiskName = DiskNvc.Get(i);
                string DiskID = DiskNvc.GetKey(i);
                double DiskValue = double.MinValue;
                string DeviceID = DiskName;

                Consejo.Monitoring.ApplicationHooks.Values.GetFreeDiskSpace(DeviceID, out FreeSpace);
                DiskValue = Convert.ToDouble(FreeSpace);
                _statusItems.Add(new ResourceStatus(DiskID, DiskValue));

            }


            NameValueCollection ServerNvc = (NameValueCollection)System.Configuration.ConfigurationManager.GetSection("AgentSettings/ServerValues");
            string ServerID = Convert.ToString(ServerNvc["1"]);
            string AgentID = Convert.ToString(ServerNvc["AgentID"]);
            _servers.Add(new ServerStatus(ServerID, _statusItems));
            _systemStatus = new SystemStatus(_servers);
            string _serverUrl = System.Configuration.ConfigurationManager.AppSettings["PanoptaUrl"] + AgentID;
            string userName = System.Configuration.ConfigurationManager.AppSettings["Username"];
            string password = System.Configuration.ConfigurationManager.AppSettings["Password"];
            string _monitorUrl = _serverUrl;
    
                this.textBox2.Text += StatusMonitor.SendStatus(_systemStatus, EncryptionType.des3, this.textBox1.Text) + Environment.NewLine + Environment.NewLine;

           
        }
    }
}